package mn.usi.changelocale;

import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import android.backup.BackupManager;
import android.content.res.Configuration;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.util.Log;
import java.util.Locale;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ChangeLocale extends BroadcastReceiver {
	private static final String TAG = "ChangeLocale";

	@Override
	public void onReceive(Context context, Intent intent) {
		String localeStr = intent.getStringExtra("locale");
		Log.i(TAG, "Received locale: " + localeStr);
		if (localeStr != null) {
			Locale locale = new Locale(localeStr);
			setLocale(locale);
			Log.i(TAG, "Update locale");
		}
	}

	public void setLocale(Locale locale) {
		try {
			IActivityManager am = ActivityManagerNative.getDefault();
			Configuration config = am.getConfiguration();

			config.locale = locale;

			config.userSetLocale = true;

			am.updateConfiguration(config);
			BackupManager.dataChanged("com.android.providers.settings");

		} catch (RemoteException e) {
		}
	}
}
